package com.thb.pay.manage.web.sys;

import com.google.common.collect.Lists;
import com.thb.pay.api.sys.vo.req.SysRolePageReq;
import com.thb.pay.api.sys.vo.req.SysRolePermissionReq;
import com.thb.pay.api.sys.vo.req.SysRoleReq;
import com.thb.pay.api.sys.vo.resp.SysPermissionVo;
import com.thb.pay.api.sys.vo.resp.SysResourceVo;
import com.thb.pay.api.sys.vo.resp.SysRoleVo;
import com.thb.pay.core.annotation.OperationLog;
import com.thb.pay.core.utils.Constant;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.core.utils.PermissionTreeBuilder;
import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.controller.BaseController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/sys/role")
public class SysRoleController extends BaseController {
    /**
     * @param id id
     * @return
     * @方法名称
     * @功能描述 查询
     * @作者: lucky
     * @创建时间: 16:31 2019/10/11
     * @版本 v1.01
     **/
    @RequestMapping("/get")
    public R get(@RequestParam("id") Long id) {
        SysRoleVo resultData = feginCommService.sysRoleServiceApi.getByRoleId(id);
        return R.okData(resultData);
    }


    /**
     * 新增
     *
     * @return
     */
    @PostMapping("/add")
    @OperationLog(value = "新增角色")
    public R add(@RequestBody SysRoleReq sysRoleReq) {
        feginCommService.sysRoleServiceApi.addRole(sysRoleReq);
        return R.ok();
    }

    /**
     * 修改
     *
     * @return
     */
    @PostMapping("/update")
    @OperationLog(value = "修改角色")
    public R update(@RequestBody SysRoleReq sysRoleReq) {
        feginCommService.sysRoleServiceApi.updateRole(sysRoleReq);
        return R.ok();
    }

    /**
     * 列表
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody SysRolePageReq pageReq) {
        PageResult<SysRoleVo> pageResult = feginCommService.sysRoleServiceApi.findPage(pageReq);
        return R.okData(pageResult);
    }

    /**
     * 删除
     *
     * @return
     */
    @PostMapping("/delete")
    @OperationLog(value = "删除角色")
    public R delete(@RequestParam("roleIds") String roleIds) {
        List<Long> targetRoleIds = parseStrIdsToList(roleIds);
        feginCommService.sysRoleServiceApi.deleteBatchRoleIds(targetRoleIds);
        return R.ok();
    }

    /**
     * 所有角色列表
     *
     * @return
     */
    @RequestMapping("/allList")
    public R allList() {
        List<SysRoleVo> resultDataList = feginCommService.sysRoleServiceApi.findAllRoles();
        return R.okData(resultDataList);
    }

    /**
     * 查看角色所有资源
     *
     * @param roleId
     * @return
     */
    @RequestMapping("/listPermissionByRoleId")
    public R listPermissionByRoleId(@RequestParam("roleId") Long roleId) {
        // 该角色对应的所有权限
        List<SysPermissionVo> sysPermissionList = feginCommService.sysPermissionServiceApi.findPermissionListByRoleId(roleId);
        // 得到系统下所有资源
        List<SysResourceVo> sysResourceList = feginCommService.sysResourceServiceApi.findListBySystemType(Constant.SYSTEM_MANAGE);
        List<PermissionTreeBuilder.Node> nodeList = Lists.newArrayList();
        for (SysResourceVo sysResource : sysResourceList) {
            PermissionTreeBuilder.Node node = new PermissionTreeBuilder.Node();
            node.setResourceId(sysResource.getId());
            node.setTitle(sysResource.getTitle());
            node.setValue(sysResource.getId() + "");
            // 设置是否被选中
            for (SysPermissionVo sysPermission : sysPermissionList) {
                if (sysResource.getId().compareTo(sysPermission.getResourceId()) == 0) {
                    node.setChecked(true);
                    break;
                }
            }
            node.setParentId(sysResource.getParentId());
            nodeList.add(node);
        }
        return R.okData(nodeList);
    }

    /**
     * 保存角色的资源
     *
     * @return
     */
    @PostMapping("/savePermission")
    @OperationLog(value = "角色分配权限")
    public R savePermission(@RequestBody SysRolePermissionReq rolePermissionReq) {
        feginCommService.sysPermissionServiceApi.saveRolePermissions(rolePermissionReq);
        return R.ok();
    }

}